﻿namespace test3

open System
open System.Management
open wmi

module vm =
    let msvm = new WmiEnum("msvm_")
    let cim = new WmiEnum("cim_")

    //////////////
    // VM
    //////////////

    let get_vm () : WmiObj seq =
        msvm?computersystem |> Seq.filter(fun x->x?Caption="Virtual Machine")

    let new_vm (name:string) : WmiObj = unbox null

    let del_vm (vm:WmiObj) = ()

    //////////////
    // SETTING
    //////////////

    let new_globalsetting () =
        let instance = (new ManagementClass("Msvm_VirtualSystemGlobalSettingData")).CreateInstance()
        new WmiObj(instance)

    let get_globalsetting (vm:WmiObj) =
        vm.assoc?Msvm_VirtualSystemGlobalSettingData
        |> Seq.head

    let get_currentsetting (vm:WmiObj) =
        vm.assoc?``msvm_VirtualSystemSettingData:Msvm_SettingsDefineState``
        |> Seq.head

    //////////////
    // RESOURCES
    //////////////

    let get_cpu (setting:WmiObj) =
        setting.assoc?Msvm_ProcessorSettingData
        |> Seq.head

    let get_mem (setting:WmiObj) =
        setting.assoc?Msvm_MemorySettingData
        |> Seq.head

    let _ide_sel (res:WmiObj) = res?ResourceSubType="Microsoft Emulated IDE Controller"
    let _scsi_sel (res:WmiObj) = res?ResourceSubType="Microsoft Synthetic SCSI Controller"

    let get_res (sel:WmiObj->bool) (setting:WmiObj) =
        setting.assoc?Msvm_ResourceAllocationSettingData
        |> Seq.filter(sel)

    let new_res (sel:WmiObj->bool) =
        msvm?ResourceAllocationSettingData
        |> Seq.filter(fun x->(string x?InstanceID).Contains(":Definition\\") && sel x)
        |> Seq.head

    let get_ide setting = get_res _ide_sel setting
    let get_scsi setting = get_res _scsi_sel setting
    let new_scsi () = new_res _scsi_sel

    let get_all_res (setting:WmiObj) =
        setting.assoc?CIM_ResourceAllocationSettingData